//
//  HOTCommChatBar.m
//  Genpara
//
//  Created by zhang chunhui on 14-9-27.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTCommChatBar.h"
#import "HOTAppDelegate.h"


@interface HOTCommChatBar () <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate>{
    
    
}

@end

@implementation HOTCommChatBar

- (void)awakeFromNib
{
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (IBAction)sendBtnClicked:(id)sender {
    
    if (self.chatTextView.text == nil || [self.chatTextView.text isEqualToString:@""]) {
        return;
    }
    
    if (_stampBtn1 && _stampBtn2) {
        //如果是带有发表情的输入框被实例化的时候，执行以下block
        if (self.sendBtnWithEmojiClicked) {
            HOTEmojiType type = HOTEmojiTypeNone;
            if (_stampBtn1.isSelected) {
                type = HOTEmojiType1;
            } else if (_stampBtn2.isSelected) {
                type = HOTEmojiType2;
            }
            
            self.sendBtnWithEmojiClicked(self.chatTextView.text, type);
        }
        
    }else {
        if (self.sendBtnClicked) {
            self.sendBtnClicked(self.chatTextView.text);
        }
    }
    self.chatTextView.text = @"";
}
- (IBAction)hiddenKeyBoardBtnClicked:(id)sender {
    
    [self.chatTextView resignFirstResponder];
}
- (IBAction)stampBtn1Clicked:(id)sender {
    
    _stampBtn2.selected = NO;
    _stampBtn1.selected = !_stampBtn1.isSelected;
}

- (IBAction)stampBtn2Clicked:(id)sender {
    
    _stampBtn1.selected = NO;
    _stampBtn2.selected = !_stampBtn2.isSelected;
}

- (IBAction)orderBtnClicked:(id)sender {
    
    if (self.orderBtnClicked) {
        
        self.orderBtnClicked();
    }
}

- (IBAction)selImageBtnClicked:(id)sender {
    
    UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:ALERT_BTN_CANCEL destructiveButtonTitle:@"カメラ" otherButtonTitles:@"写真", nil];
    
    [action showInView:[[UIApplication sharedApplication] keyWindow]];
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
    ipc.delegate = self;
    
    if (buttonIndex == 0) {
        //拍照
        [self.chatTextView resignFirstResponder];
        [ipc setSourceType:UIImagePickerControllerSourceTypeCamera];
        
    } else if (buttonIndex == 1) {
        //相册
        [self.chatTextView resignFirstResponder];
    } else {
        //取消
        return;
    }
    
    //self.topVc = (HOTBaseViewController *)((HOTAppDelegate *)[[UIApplication sharedApplication] delegate]).navc.topViewController;
    
    [self.topVc presentViewController:ipc animated:YES completion:nil];
}

#pragma mark - UIImagePickerController delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    //获取照片的原图
    UIImage *selImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    if (self.uploadImageDone) {
        
        self.uploadImageDone(selImage);
    }    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}

-(HOTEmojiType)getEmojiType {
    
    if (_stampBtn1.isSelected) {
        
        return HOTEmojiType1;
    } else if (_stampBtn2.isSelected) {
        
        return HOTEmojiType2;
    } else {
        
        return HOTEmojiTypeNone;
    }
}
@end
